# -*- coding: iso-8859-1 -*-
"""
@newfield project: Project 
@project: yatter
@newfield description: Description 
@description: Twitter Client, based on Python 2.5

@Author: U{Andreas del Galdo<mailto:andreas.delgaldo@mac.com>}
@summary: Handles the process of storing the data to the harddisk. This 
          Module is needed to handle the access to the caching system for images.
"""

import yatter.tools.network
import os
import urllib2
import logging

def storeImage(path, url):
    try:
        logging.debug("storage.store_disk.storeImage()")
        request = yatter.tools.network.createRequest(url);
        filename = url[url.rfind('/') + 1:]
        
        #create the path if not exist
        if (os.path.exists(path) == False):
            os.mkdir(path)

        image_handler = open(path + os.sep + filename,'wb')
        image_handler.write(request.read())
        image_handler.close()
        request.close()        
        return True
    except:
        logging.error("storage.store_disk.storeImage()::ERROR")
        return False
    
def loadImage(path, filename):
    try:
        logging.debug("storage.store_disk.loadImage()")
        filename = filename[filename.rfind('/') + 1:]
        if (os.path.exists(path + os.sep + filename)):
            image_handler = open(path + os.sep + filename,'rb')
            user_image = image_handler.read()
            image_handler.close()    
        return user_image
    except:
        logging.error("storage.store_disk.loadImage()::ERROR")
        return False